package org.tgcloud.group.service.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tgcloud.group.core.APPResultMessage;
import org.tgcloud.group.core.ProjectStatusEnum;
import org.tgcloud.group.core.ResultMessage;
import org.tgcloud.group.entity.MjProjectShipment;
import org.tgcloud.group.entity.MjProjectStage;
import org.tgcloud.group.repository.MjProjectShipmentMapper;
import org.tgcloud.group.repository.MjProjectStageMapper;
import org.tgcloud.group.web.controller.dto.project.ProjectQueryDto;

import javax.transaction.Transactional;
import java.util.Date;

@Service
@Slf4j
public class ProjectShipmentService {

    @Autowired
    private MjProjectShipmentMapper projectShipmentMapper;

    @Autowired
    private MjProjectStageMapper projectStageMapper;

    public APPResultMessage query(ProjectQueryDto projectQueryDto) {
        LambdaQueryWrapper<MjProjectShipment> MjProjectShipmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        MjProjectShipmentLambdaQueryWrapper.orderByAsc(MjProjectShipment::getSort);
        MjProjectShipmentLambdaQueryWrapper.eq(MjProjectShipment::getProjectId, projectQueryDto.getProjectId())
                .eq(MjProjectShipment::getStageId, projectQueryDto.getStageId());
//        if(ObjectUtils.isNotEmpty(projectQueryDto.getPageNum()) && projectQueryDto.getPageSize() != null){}
        IPage<MjProjectShipment> page = new Page<MjProjectShipment>(projectQueryDto.getPageNum(), projectQueryDto.getPageSize());
        IPage<MjProjectShipment> MjProjectShipmentIPage = projectShipmentMapper.selectPage(page, MjProjectShipmentLambdaQueryWrapper);
        APPResultMessage<Object, MjProjectShipment> message = APPResultMessage.getSuccessMessage();
        ResultMessage result = ResultMessage.builder()
                .setList(MjProjectShipmentIPage.getRecords())
                .setAllPageNumber((int) MjProjectShipmentIPage.getPages())
                .setCount(MjProjectShipmentIPage.getTotal());
        message.setResult(result);
        return message;
    }

    @Transactional
    public APPResultMessage createShipment(MjProjectShipment  mjProjectShipment) {



        //创建阶段
        MjProjectStage mjProjectStage = new MjProjectStage();
        MjProjectStage stage = projectStageMapper.selectOne(
                new LambdaQueryWrapper<MjProjectStage>()
                        .eq(MjProjectStage::getProjectId, mjProjectShipment.getProjectId())
                        .eq(MjProjectStage::getIsCurrent, 1)
        );
        if(ObjectUtils.isNotEmpty(stage) && !stage.getCode().equals(ProjectStatusEnum.WAIT_RECEIVE.getCode())){
            mjProjectStage.setProjectId(mjProjectShipment.getProjectId());
            mjProjectStage.setIsCurrent(true);
            mjProjectStage.setName(ProjectStatusEnum.WAIT_RECEIVE.getName());
            mjProjectStage.setCode(ProjectStatusEnum.WAIT_RECEIVE.getCode());
            mjProjectStage.setSort(stage.getSort() + 1);
            mjProjectStage.setCreateTime(new Date());

            stage.setIsCurrent(false);
            stage.setEndTime(new Date());

            projectStageMapper.insert(mjProjectStage);
            projectStageMapper.updateById(stage);
            mjProjectShipment.setStageId(mjProjectStage.getId());
        }


        projectShipmentMapper.insert(mjProjectShipment);
        return APPResultMessage.getSuccessMessage();
    }

    public APPResultMessage updateMjProjectShipment(MjProjectShipment  MjProjectShipment) {
        projectShipmentMapper.updateById(MjProjectShipment);
        return APPResultMessage.getSuccessMessage();
    }

    public APPResultMessage getDatils(int id) {
        MjProjectShipment MjProjectShipment = projectShipmentMapper.selectById(id);
        APPResultMessage<Object, MjProjectShipment> message = APPResultMessage.getSuccessMessage();
        ResultMessage result = ResultMessage.builder().setData(MjProjectShipment);
        message.setResult(result);
        return message;
    }

    public APPResultMessage delete(int id) {
        projectShipmentMapper.deleteById(id);
        return APPResultMessage.getSuccessMessage();
    }


    @Transactional
    public APPResultMessage sureShipment(Integer projectId) {

        //创建阶段
        MjProjectStage mjProjectStage = new MjProjectStage();
        MjProjectStage stage = projectStageMapper.selectOne(
                new LambdaQueryWrapper<MjProjectStage>()
                        .eq(MjProjectStage::getProjectId,projectId)
                        .eq(MjProjectStage::getIsCurrent, 1)
        );
//        if(ObjectUtils.isNotEmpty(stage) && !stage.getCode().equals(ProjectStatusEnum.WAIT_PACKAGE.getCode())){
            mjProjectStage.setProjectId(projectId);
            mjProjectStage.setIsCurrent(true);
            mjProjectStage.setName(ProjectStatusEnum.WAIT_EXECUTION.getName());
            mjProjectStage.setCode(ProjectStatusEnum.WAIT_EXECUTION.getCode());
            mjProjectStage.setSort(stage.getSort() + 1);
            mjProjectStage.setCreateTime(new Date());

            stage.setIsCurrent(false);
            stage.setEndTime(new Date());

            projectStageMapper.insert(mjProjectStage);
            projectStageMapper.updateById(stage);
            return APPResultMessage.getSuccessMessage();
        }


    @Transactional
    public APPResultMessage start(Integer projectId) {

        //创建阶段
        MjProjectStage mjProjectStage = new MjProjectStage();
        MjProjectStage stage = projectStageMapper.selectOne(
                new LambdaQueryWrapper<MjProjectStage>()
                        .eq(MjProjectStage::getProjectId,projectId)
                        .eq(MjProjectStage::getIsCurrent, 1)
        );
//        if(ObjectUtils.isNotEmpty(stage) && !stage.getCode().equals(ProjectStatusEnum.WAIT_PACKAGE.getCode())){
        mjProjectStage.setProjectId(projectId);
        mjProjectStage.setIsCurrent(true);
        mjProjectStage.setName(ProjectStatusEnum.WAIT_EXECUTION.getName());
        mjProjectStage.setCode(ProjectStatusEnum.WAIT_EXECUTION.getCode());
        mjProjectStage.setSort(stage.getSort() + 1);
        mjProjectStage.setCreateTime(new Date());

        stage.setIsCurrent(false);
        stage.setEndTime(new Date());

        projectStageMapper.insert(mjProjectStage);
        projectStageMapper.updateById(stage);
        return APPResultMessage.getSuccessMessage();
    }






}
